{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-06T03:12:30.137356900Z",
     "start_time": "2025-08-06T03:12:29.367515800Z"
    }
   },
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import OneHotEncoder\n",
    "import pandas as pd\n",
    "df=pd.read_csv('../data/train.csv')\n",
    "nominal_features = ['BusinessTravel','Department','EducationField','Gender','JobRole','MaritalStatus','OverTime']\n",
    "encoder = OneHotEncoder(handle_unknown='ignore', sparse_output=False)\n",
    "# 新版本的写法\n",
    "encoded = encoder.fit_transform(df[nominal_features])\n",
    "encoded_df = pd.DataFrame(encoded, columns=encoder.get_feature_names_out(nominal_features), index=df.index)\n",
    "df = df.drop(nominal_features, axis=1)\n",
    "df = pd.concat([df, encoded_df], axis=1)\n",
    "from IPython.display import display\n",
    "\n",
    "# 更灵活的显示方式\n",
    "df.to_excel('../data/wide_table.xlsx', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "13dfa4ad517fefcf"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
